package com.ruoyi.clf.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.clf.domain.*;
import com.ruoyi.clf.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.clf.service.IIntegralExchangeService;

/**
 * 积分兑换Service业务层处理
 * 
 * @author 常龙飞
 * @date 2025-05-13
 */
@Service
public class IntegralExchangeServiceImpl implements IIntegralExchangeService 
{
    @Autowired
    private IntegralExchangeMapper integralExchangeMapper;

    /**
     * 查询积分兑换
     * 
     * @param id 积分兑换主键
     * @return 积分兑换
     */
    @Override
    public IntegralExchange selectIntegralExchangeById(Long id)
    {
        return integralExchangeMapper.selectIntegralExchangeById(id);
    }

    /**
     * 查询积分兑换列表
     * 
     * @param integralExchange 积分兑换
     * @return 积分兑换
     */
    @Override
    public List<IntegralExchange> selectIntegralExchangeList(IntegralExchange integralExchange)
    {
        return integralExchangeMapper.selectIntegralExchangeList(integralExchange);
    }

    /**
     * 新增积分兑换
     * 
     * @param integralExchange 积分兑换
     * @return 结果
     */
    @Override
    public int insertIntegralExchange(IntegralExchange integralExchange)
    {
        return integralExchangeMapper.insertIntegralExchange(integralExchange);
    }

    /**
     * 修改积分兑换
     * 
     * @param integralExchange 积分兑换
     * @return 结果
     */
    @Override
    public int updateIntegralExchange(IntegralExchange integralExchange)
    {
        return integralExchangeMapper.updateIntegralExchange(integralExchange);
    }

    /**
     * 批量删除积分兑换
     * 
     * @param ids 需要删除的积分兑换主键
     * @return 结果
     */
    @Override
    public int deleteIntegralExchangeByIds(Long[] ids)
    {
        return integralExchangeMapper.deleteIntegralExchangeByIds(ids);
    }

    /**
     * 删除积分兑换信息
     * 
     * @param id 积分兑换主键
     * @return 结果
     */
    @Override
    public int deleteIntegralExchangeById(Long id)

    {
        return integralExchangeMapper.deleteIntegralExchangeById(id);
    }

// 用户小程序用户购买业务
    @Autowired
    private IntegralMapper integralMapper;

    @Autowired
    private IntegralGoodsMapper integralGoodsMapper;
    @Autowired
    private IntegralDetailsMapper integralDetailsMapper;



    @Override
    public int buyGoods(Long userid, Long goodsid,Long num,Integer addressid) {
//        判断用户积分够不够
        Integral integral = integralMapper.selectIntegralByUserId(userid);
        IntegralGoods integralGoods = integralGoodsMapper.selectIntegralGoodsById(goodsid);
        if (integral.getIntegral()<integralGoods.getExchangeIntegral()){
//            积分不够
            return 0;
        }else {
//            判断库存够不够
            if (integralGoods.getStock()<num){
//                库存不够
                return 1;
            }else {
//                库存够，积分够，购买成功
//                1.扣减库存
                integralGoods.setStock(integralGoods.getStock()-num);
                integralGoods.setSalesVolume(integralGoods.getSalesVolume()+num);
                integralGoodsMapper.updateIntegralGoods(integralGoods);
//                2.扣减积分
                integral.setIntegral(integral.getIntegral()-integralGoods.getExchangeIntegral()*num);
                integralMapper.updateIntegral(integral);
//                3.增加积分明细
                IntegralDetails integralDetails = new IntegralDetails();
                integralDetails.setUserId(userid);
                integralDetails.setStatus(1L);
                integralDetails.setTime(new Date());
                integralDetails.setContent("购买商品："+integralGoods.getGoodsName());
                integralDetails.setValue(integralGoods.getExchangeIntegral()*num);
                integralDetailsMapper.insertIntegralDetails(integralDetails);
//                4.增加兑换流水
                IntegralExchange integralExchange = new IntegralExchange();
                integralExchange.setUserId(userid);
                integralExchange.setExchangeValue(num*integralGoods.getExchangeIntegral());
                integralExchange.setExchangeGid(goodsid);
                integralExchange.setExchangeTime(new Date());
                integralExchange.setReserved1(""+num);
                integralExchange.setReserved2(""+addressid);
                integralExchangeMapper.insertIntegralExchange(integralExchange);
                return 2;
            }
        }

    }
}
